package com.lzx.demo.bloodpressurehelper.fragment.home;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

import com.lzx.demo.bloodpressurehelper.MainActivity;
import com.lzx.demo.bloodpressurehelper.R;
import com.lzx.demo.bloodpressurehelper.bean.BloodPressureBean;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by admin on 2016/10/25.
 */
public class RememBloodPressureFragment extends Fragment implements View.OnClickListener {
    private NumberPicker heightPressure;//高压
    private NumberPicker lowPressure;//低压
    private NumberPicker heartRate;//心率
    private Button clearDataDB;//清除数据按钮
    private Button saveBloodPressure;//保存数据按钮

    private SQLiteDatabase db;
    private MainActivity mainActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_home_remem_blood_pressure, container, false);
        mainActivity = (MainActivity)getActivity();
        init(view);
        return  view;
    }

    private void init(View view) {
        //设置高压范围
        heightPressure = (NumberPicker) view.findViewById(R.id.height_pressure);
        heightPressure.setMaxValue(260);
        heightPressure.setMinValue(80);
        heightPressure.setValue(120);

        //设置低压范围
        lowPressure = (NumberPicker) view.findViewById(R.id.low_pressure);
        lowPressure.setMaxValue(160);
        lowPressure.setMinValue(30);
        lowPressure.setValue(80);

        //设置心率范围
        heartRate = (NumberPicker) view.findViewById(R.id.heart_rate);
        heartRate.setMaxValue(130);
        heartRate.setMinValue(40);
        heartRate.setValue(60);

        //清除数据按钮
        clearDataDB = (Button) view.findViewById(R.id.clear_dataDB);
        clearDataDB.setOnClickListener(this);

        //保存数据按钮
        saveBloodPressure = (Button) view.findViewById(R.id.save_blood_Pressure);
        saveBloodPressure.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        String sqlStr;
        db = mainActivity.getmDBOpenHelper().getWritableDatabase();
        switch (v.getId()){
            case(R.id.clear_dataDB) ://点击清除数据按钮
                sqlStr = "DROP TABLE IF EXISTS blood";
                db.execSQL(sqlStr);
                break;
            case (R.id.save_blood_Pressure)://点击保存血压数据按钮
                //若没有表则创建表
                mainActivity.getmDBOpenHelper().createBloodPressureTable(db);
                BloodPressureBean bloodPressureBean = new BloodPressureBean(Integer.valueOf(heightPressure.getValue()),
                        Integer.valueOf(lowPressure.getValue()),
                        Integer.valueOf(heartRate.getValue()),
                        new Timestamp(new Date().getTime()));
                Toast.makeText(getContext(),"高压："+bloodPressureBean.getHeightBloodPressure()+
                        "低压："+bloodPressureBean.getLowBloodPressure()+
                        "心率："+bloodPressureBean.getHeartRate(),
                        Toast.LENGTH_SHORT)
                        .show();
                sqlStr = "INSERT INTO "
                            +" blood(HEIGHT_BLOOD_PRESSURE,LOW_BLOOD_PRESSURE,HEART_RATE,remem_time)"
                            +" VALUES(?,?,?,?);";
                db.execSQL(sqlStr,
                        new Object[]{
                                bloodPressureBean.getHeightBloodPressure(),
                                bloodPressureBean.getLowBloodPressure(),
                                bloodPressureBean.getHeartRate(),
                                bloodPressureBean.getRemem_time()
                        });
                break;
        }
    }
}
